package com.bangyo.common.spider.impl;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.Tag;
import org.htmlparser.lexer.Lexer;
import org.htmlparser.tags.Div;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.tags.Span;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import org.htmlparser.util.ParserUtils;
import org.htmlparser.visitors.NodeVisitor;

import com.bangyo.common.spider.Spider;
import com.bangyo.stuff.Link;

public class TaobaoFavoriteSpider implements Spider<List<Link>>
{

    public List<Link> spy(String url) throws IOException
    {
        try
        {
            Parser parser = new Parser(new Lexer(new URL(url).openConnection()));
            NodeList nodeList = parser.parse(new NodeFilter()
            {
                private static final long serialVersionUID = -5814494472470667343L;

                public boolean accept(Node node)
                {
                    if (node instanceof Div)
                    {
                        Div div = (Div)node;
                        String id = div.getAttribute("id");
                        if (null!=id)
                        {
                            return id.equals("FavoritesList");
                        }
                    }
                    return false;
                }
            });
            int size = nodeList.size();
            List<Link> links = new ArrayList<Link>(size);
            for (int i = 0; i<size; i++)
            {
                Node node = nodeList.elementAt(i);
                if (node instanceof Div)
                {
                    this.generateLink(node, links);
                }
            }
            return links;
        }
        catch (ParserException e)
        {
            e.printStackTrace();
        }
        return Collections.emptyList();
    }

    private void generateLink(Node node, final List<Link> links)
    {
        final Link l = new Link();
        node.accept(new NodeVisitor()
        {
            @Override
            public void visitTag(Tag tag)
            {
                if ("H3".equals(tag.getTagName()))
                {
                    Node[] as = ParserUtils.findTypeInNode(tag, LinkTag.class);
                    if (null!=as&&as.length>0)
                    {
                        LinkTag a = (LinkTag)as[0];
                        l.setTitle(a.getLinkText());
                        l.setTitleLink(a.getLink());
                        links.add(l);
                    }
                }
                else if ("FavorItemInfo".equals(tag.getAttribute("class")))
                {
                    Node[] spans = ParserUtils.findTypeInNode(tag, Span.class);
                    if (null!=spans&&spans.length>=3)
                    {
                        l.setDescription(((Span)spans[0]).toPlainTextString()
                            .replaceAll("\\s", "")
                            +((Span)spans[2]).toPlainTextString().replaceAll(
                                "\\s", ""));
                    }
                }
            }
        });
        return;
    }

    public static void main(String[] args) throws IOException
    {
        TaobaoFavoriteSpider spider = new TaobaoFavoriteSpider();
        List<Link> links = spider
            .spy("http://favorite.taobao.com/collectList.htm?usernick=velna_007");
        for (Link link:links)
        {
            System.out.println(link.getTitle()+" : "+link.getTitleLink()+" : "
                +link.getDescription());
        }
    }
}
